package com.dongdongshop.service;

import com.dongdongshop.log.DongdongLog;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.pojo.VO.TbSpecificationVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class TbSpecificationServiceImpl implements TbSpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper optionMapper;

    @Override
    public List<TbSpecification> selectBy(TbSpecification tbSpecification) {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectBy(tbSpecification);
        return tbSpecifications;
    }


    @Override
    @DongdongLog
    public boolean deleteById(Integer[] ids) {
        //删除规格表 规格选项表
        tbSpecificationMapper.deleteByIds(ids);
        optionMapper.deleteByIds(ids);
        return true;
    }

    @Override
    public boolean insertBy(List<TbSpecificationOption> options, TbSpecification tbSpecification) {
        //增加主表返回主键 增加附表
        tbSpecificationMapper.insert(tbSpecification);
        optionMapper.insertBy(options,tbSpecification.getId());
        return true;
    }

    @Override
    public List<TbSpecificationVO> selectByVO(Long id) {
      List<TbSpecificationVO> voList =  tbSpecificationMapper.selectById(id);
      return voList;
    }

    @Override
    public boolean updateBy(List<TbSpecificationOption> options, TbSpecification tbSpecification) {
        //修改主表  删除附表再增加
        tbSpecificationMapper.updateByPrimaryKey(tbSpecification);

        TbSpecificationOptionExample toe = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        optionMapper.deleteByExample(toe);

        optionMapper.insertBy(options,tbSpecification.getId());

        return true;
    }

    @Override
    public List<TbSpecification> selectList() {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(null);
        return tbSpecifications;
    }

    @Override
    public List<TbSpecificationOption> selectBySpecId(Long id) {
        TbSpecificationOptionExample tb = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = tb.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> list = optionMapper.selectByExample(tb);
        return list;
    }


}
